package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.OutStockMapper;
import com.goods.business.service.*;
import com.goods.common.model.business.*;
import com.goods.common.vo.business.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/25 9:00
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class OutStockServiceImpl implements OutStockService {

    @Autowired
    private OutStockMapper outStockMapper;
    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ProductService productService;
    @Autowired
    private OutStockInfoService outStockInfoService;
    @Autowired
    private ProductStockService productStockService;


    @Override
    public PageInfo<OutStockVO> findOutStockList(String pageNum, String pageSize, String status, String type, String outNum) {
        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        OutStockVO outStockVO = new OutStockVO();
        if(!StringUtils.isEmpty(status)){
            outStockVO.setStatus(Integer.parseInt(status));
        }
        if(!StringUtils.isEmpty(type)){
            outStockVO.setType(Integer.parseInt(type));
        }
        if(!StringUtils.isEmpty(outNum)){
            outStockVO.setOutNum(outNum);
        }
        List<OutStockVO> list = outStockMapper.selectOutStockVOByCriteria(outStockVO);


        return new PageInfo<OutStockVO>(list);
    }

    @Override
    public PageInfo<OutStockItemVO> detail(Long id, String pageNum) {
        PageHelper.startPage(Integer.parseInt(pageNum),10);
        List<OutStockItemVO> list = outStockMapper.detail(id);
        return new PageInfo<OutStockItemVO>(list);
    }

    @Override
    public OutStock findOneOutStock(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        return outStock;
    }

    @Override
    public void remove(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(1);
        outStockMapper.updateByPrimaryKeySelective(outStock);
    }

    @Override
    public void back(Long id) {
        OutStock outStock = new OutStock();
        outStock.setId(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKeySelective(outStock);
    }

    @Override
    public void publish(Long id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock);
        String outNum = outStock.getOutNum();
        List<OutStockInfo> list = outStockInfoService.getList(outNum);
        for (OutStockInfo outStockInfo : list) {
            productStockService.sub(outStockInfo);
        }
    }

    @Override
    public void delete(Long id) {
        outStockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(OutStockVO outStockVO, String username) {
        if (outStockVO.getConsumerId() == null) {
            //使用未知来源
            ConsumerVO consumerVO = new ConsumerVO();
            consumerVO.setAddress(outStockVO.getAddress());
            consumerVO.setName(outStockVO.getName());
            consumerVO.setPhone(outStockVO.getPhone());
            consumerVO.setSort(outStockVO.getSort());
            consumerVO.setContact(outStockVO.getContact());
            Consumer consumer = consumerService.add(consumerVO);
            Long id = consumer.getId();
            outStockVO.setConsumerId(id);
        }
        List<Object> products = outStockVO.getProducts();
        String outNum = UUID.randomUUID().toString().replaceAll("-", "");
        int count = 0;
        for (Object productO : products) {
            Map<String, Integer> product = (Map<String, Integer>) productO;
            Integer productId = (Integer) product.get("productId");
            Integer productNumber = (Integer) product.get("productNumber");
            ProductVO productVO = productService.edit(Long.parseLong(productId + ""));
            String pNum = productVO.getPNum();
            OutStockInfo outStockInfo = new OutStockInfo();
            outStockInfo.setpNum(pNum);
            outStockInfo.setOutNum(outNum);
            outStockInfo.setProductNumber(productNumber);
            outStockInfo.setCreateTime(new Date());
            outStockInfo.setModifiedTime(new Date());
            outStockInfoService.add(outStockInfo);
            count += productNumber;
        }
        OutStock outStock = new OutStock();
        BeanUtils.copyProperties(outStockVO, outStock);
        outStock.setOutNum(outNum);
        outStock.setOperator(username);
        outStock.setProductNumber(count);
        outStock.setCreateTime(new Date());
        outStock.setStatus(2);

        outStockMapper.insert(outStock);

    }
}
